package com.badibadi.activity;

import com.badibadi.fragment.RecordFragment1;
import com.badibadi.fragment.RecordFragment2;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

/**  
 * 我的记录列表 2014-8-26 主要 my_record_record my_record_record
 * 
 * @author Administrator
 * 
 */
public class RecordMyRecordListActivity extends BaseActivity {
	private static final String TAG = "RecordMyRecordListActivity";
	private TabHost mtabhost;
	private RelativeLayout bottom_btn1, bottom_btn2;
	public FragmentManager manager;
	public String uid = "1";
	public String fid = "-1";
	private String showType;
	// public MyRecordList0Fragment mList0Fragment = new
	// MyRecordList0Fragment();
	public com.badibadi.fragment.RecordFragment0 recordFragment0;
	public RecordFragment1 recordFragment1;
	public com.badibadi.fragment.RecordFragment2 recordFragment2;

	// private Handler handler = new Handler() {
	// @Override
	// public void handleMessage(Message msg) {
	// // TODO Auto-generated method stub
	// switch (msg.what) {
	// case 1:
	//
	// break;
	//
	// default:
	// break;
	// }
	// }
	// };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.my_record_list);
		TextView my_record_tiltle = (TextView) this
				.findViewById(R.id.my_record_tiltle);
		my_record_tiltle.setText(getResources().getString(R.string.l_xb137));
		uid = getIntent().getStringExtra("uid");
		fid = getIntent().getStringExtra("fid");
		if (uid == null) {
			uid = "1";
		}
		if (fid == null) {
			fid = "-1";
		}
		init();
		initFragment();
	}

	/**
	 * 根据num来读取framgent
	 * 
	 * @param num
	 */
	protected void LoadingFragment(int num, String uid, String fid) {
		FragmentTransaction transaction = manager.beginTransaction();
		switch (num) {
		case 0:
			recordFragment0 = com.badibadi.fragment.RecordFragment0
					.getInstance();
			Bundle bun = new Bundle();
			bun.putString("uid", uid);
			bun.putString("fid", fid);
//			   
			bun.putString("showType", showType);
			recordFragment0.setArguments(bun);
			transaction.replace(R.id.my_record_layout, recordFragment0);
			transaction.commit();
			break;
		case 1:
			recordFragment1 = RecordFragment1.getInstance();
			Bundle bun1 = new Bundle();
			bun1.putString("uid", uid);
			bun1.putString("fid", fid);
			bun1.putString("showType", showType);
			recordFragment1.setArguments(bun1);
			transaction.replace(R.id.my_record_layout, recordFragment1);
			transaction.commit();
			break;
		case 2:
			recordFragment2 = RecordFragment2.getInstance();
			Bundle bun2 = new Bundle();
			bun2.putString("uid", uid);
			bun2.putString("fid", fid);
			bun2.putString("showType", showType);
			recordFragment2.setArguments(bun2);
			transaction.replace(R.id.my_record_layout, recordFragment2);
			transaction.commit();
			break;
		}

		// holder.transaction.replace(R.id.my_record_layout, mList0Fragment);
		// holder.transaction.commit();
	}

	/**
	 * 初始化fragment
	 */
	protected void initFragment() {
		// TODO Auto-generated method stub
		// ViewFragmentHolder holder = new ViewFragmentHolder();
		// MyRecordList0Fragment mList0Fragment = new MyRecordList0Fragment();
		// holder.transaction.replace(R.id.my_record_layout, mList0Fragment);
		// holder.transaction.commit();
		LoadingFragment(0, uid, fid);
	}

	private void init() {
		// TODO Auto-generated method stub
		((TextView) (this.findViewById(R.id.my_record_tiltle)))
				.setText(getResources().getString(R.string.MyRecord));
		manager = getSupportFragmentManager();
		this.findViewById(R.id.my_record_list_return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		mtabhost = (TabHost) findViewById(R.id.my_record_tabhost);
		// mTabHost1.
		// tabHost=getTabHost();
		mtabhost.setup();// 放在获取地址的后面
		mtabhost.addTab(mtabhost
				.newTabSpec("my_record one")
				.setIndicator(getResources().getString(R.string.Newest),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		mtabhost.addTab(mtabhost
				.newTabSpec("my_record two")
				.setIndicator(getResources().getString(R.string.Hot),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		mtabhost.addTab(mtabhost
				.newTabSpec("my_record three")
				.setIndicator(getResources().getString(R.string.VoteRanking),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		TabHostUtils.SetTabHost(this, mtabhost);
		mtabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				switch (mtabhost.getCurrentTab()) {
				case 0:
					LoadingFragment(0, uid, fid);
					break;
				case 1:
					LoadingFragment(1, uid, fid);
					break;
				case 2:
					LoadingFragment(2, uid, fid);
					break;
				}
			}
		});
		bottom_btn1 = (RelativeLayout) findViewById(R.id.my_record_bottom_btn1);
		bottom_btn2 = (RelativeLayout) findViewById(R.id.my_record_bottom_btn2);
		bottom_btn1.setSelected(true);
		showType = "myRecord";
		this.findViewById(R.id.my_record_record).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(
								RecordMyRecordListActivity.this,
								AddingRecordNewActivity.class);
						startActivity(intent);

						// Utils.showMessage(RecordMyRecordListActivity.this,
						// "成功");
					}
				});
	}

	/**
	 * 底部按钮的点击事件
	 * 
	 * @param view
	 */
	public void OnClick0(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn2.setSelected(false);
			showType = "myRecord";
			mtabhost.setCurrentTab(0);
			LoadingFragment(0, uid, fid);
		}
	}

	/**
	 * 底部按钮的点击事件
	 * 
	 * @param view
	 */
	public void OnClick1(View view) {
		if (view.isSelected() != true) {
			view.setSelected(true);
			bottom_btn1.setSelected(false);
			showType = "collectRecord";
			mtabhost.setCurrentTab(0);
			LoadingFragment(0, uid, fid);
		}
	}

}